import 'menu_item_model.dart';

class SideMenuController {
  // 当前展开的一级菜单ID
  String? expandedLevelOneId;
  
  // 当前选中的菜单项ID
  String? selectedMenuId;
  
  // 当前悬停的二级菜单ID
  String? hoveredLevelTwoId;
  
  // 菜单数据
  final List<MenuItemModel> menuItems;
  
  // 状态更新回调
  final Function() onStateChanged;
  
  SideMenuController({
    required this.menuItems,
    required this.onStateChanged,
  });
  
  // 切换一级菜单展开状态
  void toggleLevelOne(String id) {
    if (expandedLevelOneId == id) {
      expandedLevelOneId = null;
    } else {
      expandedLevelOneId = id;
    }
    onStateChanged();
  }
  
  // 选择菜单项
  void selectMenuItem(String id, {String? route}) {
    selectedMenuId = id;
    onStateChanged();
  }
  
  // 设置悬停的二级菜单
  void setHoveredLevelTwo(String? id) {
    hoveredLevelTwoId = id;
    onStateChanged();
  }
  
  // 检查一级菜单是否展开
  bool isLevelOneExpanded(String id) {
    return expandedLevelOneId == id;
  }
  
  // 检查菜单项是否被选中
  bool isMenuItemSelected(String id) {
    return selectedMenuId == id;
  }
  
  // 检查菜单项是否有被选中的子菜单（递归）
  bool hasSelectedChild(MenuItemModel menuItem) {
    if (menuItem.children.isEmpty) {
      return false;
    }
    
    for (final child in menuItem.children) {
      if (isMenuItemSelected(child.id)) {
        return true;
      }
      
      if (hasSelectedChild(child)) {
        return true;
      }
    }
    
    return false;
  }
  
  // 检查二级菜单是否被悬停
  bool isLevelTwoHovered(String id) {
    return hoveredLevelTwoId == id;
  }
}